package control

import (
	"database/sql"

	"crossrun.top/model"
)

func ListTag() []model.Tag {
	rows, err := db.Query("SELECT * FROM tag ")
	defer rows.Close()
	checkErr(err)
	return processTag(rows)
}

func ListTagById(id int) []model.Tag {
	rows, err := db.Query("SELECT * FROM tag where id = ?", id)
	defer rows.Close()
	checkErr(err)
	return processTag(rows)
}

func listTagByName(name string) []model.Tag {
	rows, err := db.Query("SELECT * FROM tag where name = ?", name)
	defer rows.Close()
	checkErr(err)
	return processTag(rows)
}

func processTag(rows *sql.Rows) []model.Tag {
	var tags []model.Tag
	for rows.Next() {
		var name string
		var id string
		rows.Scan(&name, &id)
		tags = append(tags, model.Tag{name, id})
	}
	return tags
}
